<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Encode - character encodings in Perl - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/Encode">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Encode.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/Encode">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/Encode">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/Encode">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/Encode">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/Encode">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/Encode">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/Encode">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/Encode">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/Encode">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/Encode">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/Encode">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/Encode">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/Encode">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/Encode">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/Encode">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/Encode">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/Encode">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/Encode">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/Encode">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/Encode">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/Encode">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/Encode">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/Encode">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/Encode">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/Encode">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/Encode">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/Encode">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/Encode">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/Encode">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/Encode">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/Encode">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/Encode">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/Encode">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/Encode">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/Encode">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/Encode">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/Encode">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/Encode">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/Encode">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/Encode">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/Encode">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/Encode">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/Encode">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/Encode">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/Encode">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/Encode">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/Encode">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/Encode">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/Encode">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/Encode">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/Encode">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/Encode">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/Encode">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/Encode">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/Encode">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/Encode">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/Encode">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/Encode">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/Encode">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/Encode">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/Encode">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/Encode">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/Encode">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/Encode">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/Encode">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/Encode">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/Encode">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/Encode">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/Encode">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/Encode">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/Encode">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/Encode">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/Encode">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/Encode">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/Encode">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/Encode">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/Encode">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/Encode">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/Encode">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/Encode">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Encode.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/Encode">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/Encode">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/Encode">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/Encode">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/Encode">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/Encode">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/Encode">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/Encode">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/Encode">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/Encode">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/Encode">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/Encode">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/Encode">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/Encode">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/Encode">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/Encode">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/Encode">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/Encode">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/Encode">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/Encode">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/Encode">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/Encode">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/Encode">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/Encode">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/Encode">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/Encode">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/Encode">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/Encode">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/Encode">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/Encode">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/Encode">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/Encode">5.35.1</a>
          <a class="dropdown-item active" href="Encode.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Encode.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="Encode.html">Encode</a>
          <div id="more">
            (<a href="Encode.txt">source</a>,
            <a href="https://metacpan.org/pod/Encode">CPAN</a>)
          </div>
            <div id="moduleversion">version 3.08</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#SYNOPSIS">SYNOPSIS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#Table-of-Contents">Table of Contents</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#DESCRIPTION">DESCRIPTION</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#TERMINOLOGY">TERMINOLOGY</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#character">character</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#byte">byte</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#octet">octet</a>
              </li>
          </ul>

              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#THE-PERL-ENCODING-API">THE PERL ENCODING API</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#Basic-methods">Basic methods</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#encode">encode</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#decode">decode</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#find_encoding">find_encoding</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#find_mime_encoding">find_mime_encoding</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#from_to">from_to</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#encode_utf8">encode_utf8</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#decode_utf8">decode_utf8</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#Listing-available-encodings">Listing available encodings</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#Defining-Aliases">Defining Aliases</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#Finding-IANA-Character-Set-Registry-names">Finding IANA Character Set Registry names</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#Encoding-via-PerlIO">Encoding via PerlIO</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#Handling-Malformed-Data">Handling Malformed Data</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#List-of-CHECK-values">List of CHECK values</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#FB_DEFAULT">FB_DEFAULT</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#FB_CROAK">FB_CROAK</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#FB_QUIET">FB_QUIET</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#FB_WARN">FB_WARN</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#FB_PERLQQ-FB_HTMLCREF-FB_XMLCREF">FB_PERLQQ FB_HTMLCREF FB_XMLCREF</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#The-bitmask">The bitmask</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#LEAVE_SRC">LEAVE_SRC</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#coderef-for-CHECK">coderef for CHECK</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#Defining-Encodings">Defining Encodings</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#The-UTF8-flag">The UTF8 flag</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#Messing-with-Perl&#39;s-Internals">Messing with Perl&#39;s Internals</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Encode.html#is_utf8">is_utf8</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#_utf8_on">_utf8_on</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#_utf8_off">_utf8_off</a>
              </li>
          </ul>

              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#UTF-8-vs.-utf8-vs.-UTF8">UTF-8 vs. utf8 vs. UTF8</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#SEE-ALSO">SEE ALSO</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#MAINTAINER">MAINTAINER</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Encode.html#COPYRIGHT">COPYRIGHT</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="Encode.html#NAME">#</a>NAME</h1>

<p>Encode - character encodings in Perl</p>

<h1 id="SYNOPSIS"><a class="permalink" href="Encode.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<pre><code>use Encode qw(decode encode);
$characters = decode(&#39;UTF-8&#39;, $octets,     Encode::FB_CROAK);
$octets     = encode(&#39;UTF-8&#39;, $characters, Encode::FB_CROAK);</code></pre>

<h2 id="Table-of-Contents"><a class="permalink" href="Encode.html#Table-of-Contents">#</a><a id="Table"></a>Table of Contents</h2>

<p>Encode consists of a collection of modules whose details are too extensive to fit in one document. This one itself explains the top-level APIs and general topics at a glance. For other topics and more details, see the documentation for these modules:</p>

<dl>

<dt id="Encode::Alias-Alias-definitions-to-encodings"><a class="permalink" href="Encode.html#Encode::Alias-Alias-definitions-to-encodings">#</a><a id="Encode"></a><a id="Encode::Alias---Alias-definitions-to-encodings"></a><a href="./Encode::Alias.html">Encode::Alias</a> - Alias definitions to encodings</dt>
<dd>

</dd>
<dt id="Encode::Encoding-Encode-Implementation-Base-Class"><a class="permalink" href="Encode.html#Encode::Encoding-Encode-Implementation-Base-Class">#</a><a id="Encode1"></a><a id="Encode::Encoding---Encode-Implementation-Base-Class"></a><a href="./Encode::Encoding.html">Encode::Encoding</a> - Encode Implementation Base Class</dt>
<dd>

</dd>
<dt id="Encode::Supported-List-of-Supported-Encodings"><a class="permalink" href="Encode.html#Encode::Supported-List-of-Supported-Encodings">#</a><a id="Encode2"></a><a id="Encode::Supported---List-of-Supported-Encodings"></a><a href="./Encode::Supported.html">Encode::Supported</a> - List of Supported Encodings</dt>
<dd>

</dd>
<dt id="Encode::CN-Simplified-Chinese-Encodings"><a class="permalink" href="Encode.html#Encode::CN-Simplified-Chinese-Encodings">#</a><a id="Encode3"></a><a id="Encode::CN---Simplified-Chinese-Encodings"></a><a href="./Encode::CN.html">Encode::CN</a> - Simplified Chinese Encodings</dt>
<dd>

</dd>
<dt id="Encode::JP-Japanese-Encodings"><a class="permalink" href="Encode.html#Encode::JP-Japanese-Encodings">#</a><a id="Encode4"></a><a id="Encode::JP---Japanese-Encodings"></a><a href="./Encode::JP.html">Encode::JP</a> - Japanese Encodings</dt>
<dd>

</dd>
<dt id="Encode::KR-Korean-Encodings"><a class="permalink" href="Encode.html#Encode::KR-Korean-Encodings">#</a><a id="Encode5"></a><a id="Encode::KR---Korean-Encodings"></a><a href="./Encode::KR.html">Encode::KR</a> - Korean Encodings</dt>
<dd>

</dd>
<dt id="Encode::TW-Traditional-Chinese-Encodings"><a class="permalink" href="Encode.html#Encode::TW-Traditional-Chinese-Encodings">#</a><a id="Encode6"></a><a id="Encode::TW---Traditional-Chinese-Encodings"></a><a href="./Encode::TW.html">Encode::TW</a> - Traditional Chinese Encodings</dt>
<dd>

</dd>
</dl>

<h1 id="DESCRIPTION"><a class="permalink" href="Encode.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>The <code>Encode</code> module provides the interface between Perl strings and the rest of the system. Perl strings are sequences of <i>characters</i>.</p>

<p>The repertoire of characters that Perl can represent is a superset of those defined by the Unicode Consortium. On most platforms the ordinal values of a character as returned by <code>ord(<i>S</i>)</code> is the <i>Unicode codepoint</i> for that character. The exceptions are platforms where the legacy encoding is some variant of EBCDIC rather than a superset of ASCII; see <a href="perlebcdic.html">perlebcdic</a>.</p>

<p>During recent history, data is moved around a computer in 8-bit chunks, often called &quot;bytes&quot; but also known as &quot;octets&quot; in standards documents. Perl is widely used to manipulate data of many types: not only strings of characters representing human or computer languages, but also &quot;binary&quot; data, being the machine&#39;s representation of numbers, pixels in an image, or just about anything.</p>

<p>When Perl is processing &quot;binary data&quot;, the programmer wants Perl to process &quot;sequences of bytes&quot;. This is not a problem for Perl: because a byte has 256 possible values, it easily fits in Perl&#39;s much larger &quot;logical character&quot;.</p>

<p>This document mostly explains the <i>how</i>. <a href="perlunitut.html">perlunitut</a> and <a href="perlunifaq.html">perlunifaq</a> explain the <i>why</i>.</p>

<h2 id="TERMINOLOGY"><a class="permalink" href="Encode.html#TERMINOLOGY">#</a>TERMINOLOGY</h2>

<h3 id="character"><a class="permalink" href="Encode.html#character">#</a>character</h3>

<p>A character in the range 0 .. 2**32-1 (or more); what Perl&#39;s strings are made of.</p>

<h3 id="byte"><a class="permalink" href="Encode.html#byte">#</a>byte</h3>

<p>A character in the range 0..255; a special case of a Perl character.</p>

<h3 id="octet"><a class="permalink" href="Encode.html#octet">#</a>octet</h3>

<p>8 bits of data, with ordinal values 0..255; term for bytes passed to or from a non-Perl context, such as a disk file, standard I/O stream, database, command-line argument, environment variable, socket etc.</p>

<h1 id="THE-PERL-ENCODING-API"><a class="permalink" href="Encode.html#THE-PERL-ENCODING-API">#</a><a id="THE"></a>THE PERL ENCODING API</h1>

<h2 id="Basic-methods"><a class="permalink" href="Encode.html#Basic-methods">#</a><a id="Basic"></a>Basic methods</h2>

<h3 id="encode"><a class="permalink" href="Encode.html#encode">#</a>encode</h3>

<pre><code>$octets  = encode(ENCODING, STRING[, CHECK])</code></pre>

<p>Encodes the scalar value <i>STRING</i> from Perl&#39;s internal form into <i>ENCODING</i> and returns a sequence of octets. <i>ENCODING</i> can be either a canonical name or an alias. For encoding names and aliases, see <a href="Encode.html#Defining-Aliases">&quot;Defining Aliases&quot;</a>. For CHECK, see <a href="Encode.html#Handling-Malformed-Data">&quot;Handling Malformed Data&quot;</a>.</p>

<p><b>CAVEAT</b>: the input scalar <i>STRING</i> might be modified in-place depending on what is set in CHECK. See <a href="Encode.html#LEAVE_SRC">&quot;LEAVE_SRC&quot;</a> if you want your inputs to be left unchanged.</p>

<p>For example, to convert a string from Perl&#39;s internal format into ISO-8859-1, also known as Latin1:</p>

<pre><code>$octets = encode(&quot;iso-8859-1&quot;, $string);</code></pre>

<p><b>CAVEAT</b>: When you run <code>$octets = encode(&quot;UTF-8&quot;, $string)</code>, then $octets <i>might not be equal to</i> $string. Though both contain the same data, the UTF8 flag for $octets is <i>always</i> off. When you encode anything, the UTF8 flag on the result is always off, even when it contains a completely valid UTF-8 string. See <a href="Encode.html#The-UTF8-flag">&quot;The UTF8 flag&quot;</a> below.</p>

<p>If the $string is <code>undef</code>, then <code>undef</code> is returned.</p>

<p><code>str2bytes</code> may be used as an alias for <code>encode</code>.</p>

<h3 id="decode"><a class="permalink" href="Encode.html#decode">#</a>decode</h3>

<pre><code>$string = decode(ENCODING, OCTETS[, CHECK])</code></pre>

<p>This function returns the string that results from decoding the scalar value <i>OCTETS</i>, assumed to be a sequence of octets in <i>ENCODING</i>, into Perl&#39;s internal form. As with encode(), <i>ENCODING</i> can be either a canonical name or an alias. For encoding names and aliases, see <a href="Encode.html#Defining-Aliases">&quot;Defining Aliases&quot;</a>; for <i>CHECK</i>, see <a href="Encode.html#Handling-Malformed-Data">&quot;Handling Malformed Data&quot;</a>.</p>

<p><b>CAVEAT</b>: the input scalar <i>OCTETS</i> might be modified in-place depending on what is set in CHECK. See <a href="Encode.html#LEAVE_SRC">&quot;LEAVE_SRC&quot;</a> if you want your inputs to be left unchanged.</p>

<p>For example, to convert ISO-8859-1 data into a string in Perl&#39;s internal format:</p>

<pre><code>$string = decode(&quot;iso-8859-1&quot;, $octets);</code></pre>

<p><b>CAVEAT</b>: When you run <code>$string = decode(&quot;UTF-8&quot;, $octets)</code>, then $string <i>might not be equal to</i> $octets. Though both contain the same data, the UTF8 flag for $string is on. See <a href="Encode.html#The-UTF8-flag">&quot;The UTF8 flag&quot;</a> below.</p>

<p>If the $string is <code>undef</code>, then <code>undef</code> is returned.</p>

<p><code>bytes2str</code> may be used as an alias for <code>decode</code>.</p>

<h3 id="find_encoding"><a class="permalink" href="Encode.html#find_encoding">#</a>find_encoding</h3>

<pre><code>[$obj =] find_encoding(ENCODING)</code></pre>

<p>Returns the <i>encoding object</i> corresponding to <i>ENCODING</i>. Returns <code>undef</code> if no matching <i>ENCODING</i> is find. The returned object is what does the actual encoding or decoding.</p>

<pre><code>$string = decode($name, $bytes);</code></pre>

<p>is in fact</p>

<pre><code>$string = do {
    $obj = find_encoding($name);
    croak qq(encoding &quot;$name&quot; not found) unless ref $obj;
    $obj-&gt;decode($bytes);
};</code></pre>

<p>with more error checking.</p>

<p>You can therefore save time by reusing this object as follows;</p>

<pre><code>my $enc = find_encoding(&quot;iso-8859-1&quot;);
while(&lt;&gt;) {
    my $string = $enc-&gt;decode($_);
    ... # now do something with $string;
}</code></pre>

<p>Besides <a href="Encode.html#decode">&quot;decode&quot;</a> and <a href="Encode.html#encode">&quot;encode&quot;</a>, other methods are available as well. For instance, <code>name()</code> returns the canonical name of the encoding object.</p>

<pre><code>find_encoding(&quot;latin1&quot;)-&gt;name; # iso-8859-1</code></pre>

<p>See <a href="./Encode::Encoding.html">Encode::Encoding</a> for details.</p>

<h3 id="find_mime_encoding"><a class="permalink" href="Encode.html#find_mime_encoding">#</a>find_mime_encoding</h3>

<pre><code>[$obj =] find_mime_encoding(MIME_ENCODING)</code></pre>

<p>Returns the <i>encoding object</i> corresponding to <i>MIME_ENCODING</i>. Acts same as <code>find_encoding()</code> but <code>mime_name()</code> of returned object must match to <i>MIME_ENCODING</i>. So as opposite of <code>find_encoding()</code> canonical names and aliases are not used when searching for object.</p>

<pre><code>find_mime_encoding(&quot;utf8&quot;); # returns undef because &quot;utf8&quot; is not valid I&lt;MIME_ENCODING&gt;
find_mime_encoding(&quot;utf-8&quot;); # returns encode object &quot;utf-8-strict&quot;
find_mime_encoding(&quot;UTF-8&quot;); # same as &quot;utf-8&quot; because I&lt;MIME_ENCODING&gt; is case insensitive
find_mime_encoding(&quot;utf-8-strict&quot;); returns undef because &quot;utf-8-strict&quot; is not valid I&lt;MIME_ENCODING&gt;</code></pre>

<h3 id="from_to"><a class="permalink" href="Encode.html#from_to">#</a>from_to</h3>

<pre><code>[$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])</code></pre>

<p>Converts <i>in-place</i> data between two encodings. The data in $octets must be encoded as octets and <i>not</i> as characters in Perl&#39;s internal format. For example, to convert ISO-8859-1 data into Microsoft&#39;s CP1250 encoding:</p>

<pre><code>from_to($octets, &quot;iso-8859-1&quot;, &quot;cp1250&quot;);</code></pre>

<p>and to convert it back:</p>

<pre><code>from_to($octets, &quot;cp1250&quot;, &quot;iso-8859-1&quot;);</code></pre>

<p>Because the conversion happens in place, the data to be converted cannot be a string constant: it must be a scalar variable.</p>

<p><code>from_to()</code> returns the length of the converted string in octets on success, and <code>undef</code> on error.</p>

<p><b>CAVEAT</b>: The following operations may look the same, but are not:</p>

<pre><code>from_to($data, &quot;iso-8859-1&quot;, &quot;UTF-8&quot;); #1
$data = decode(&quot;iso-8859-1&quot;, $data);  #2</code></pre>

<p>Both #1 and #2 make $data consist of a completely valid UTF-8 string, but only #2 turns the UTF8 flag on. #1 is equivalent to:</p>

<pre><code>$data = encode(&quot;UTF-8&quot;, decode(&quot;iso-8859-1&quot;, $data));</code></pre>

<p>See <a href="Encode.html#The-UTF8-flag">&quot;The UTF8 flag&quot;</a> below.</p>

<p>Also note that:</p>

<pre><code>from_to($octets, $from, $to, $check);</code></pre>

<p>is equivalent to:</p>

<pre><code>$octets = encode($to, decode($from, $octets), $check);</code></pre>

<p>Yes, it does <i>not</i> respect the $check during decoding. It is deliberately done that way. If you need minute control, use <code>decode</code> followed by <code>encode</code> as follows:</p>

<pre><code>$octets = encode($to, decode($from, $octets, $check_from), $check_to);</code></pre>

<h3 id="encode_utf8"><a class="permalink" href="Encode.html#encode_utf8">#</a>encode_utf8</h3>

<pre><code>$octets = encode_utf8($string);</code></pre>

<p>Equivalent to <code>$octets = encode(&quot;utf8&quot;, $string)</code>. The characters in $string are encoded in Perl&#39;s internal format, and the result is returned as a sequence of octets. Because all possible characters in Perl have a (loose, not strict) utf8 representation, this function cannot fail.</p>

<p><b>WARNING</b>: do not use this function for data exchange as it can produce not strict utf8 $octets! For strictly valid UTF-8 output use <code>$octets = encode(&quot;UTF-8&quot;, $string)</code>.</p>

<h3 id="decode_utf8"><a class="permalink" href="Encode.html#decode_utf8">#</a>decode_utf8</h3>

<pre><code>$string = decode_utf8($octets [, CHECK]);</code></pre>

<p>Equivalent to <code>$string = decode(&quot;utf8&quot;, $octets [, CHECK])</code>. The sequence of octets represented by $octets is decoded from (loose, not strict) utf8 into a sequence of logical characters. Because not all sequences of octets are valid not strict utf8, it is quite possible for this function to fail. For CHECK, see <a href="Encode.html#Handling-Malformed-Data">&quot;Handling Malformed Data&quot;</a>.</p>

<p><b>WARNING</b>: do not use this function for data exchange as it can produce $string with not strict utf8 representation! For strictly valid UTF-8 $string representation use <code>$string = decode(&quot;UTF-8&quot;, $octets [, CHECK])</code>.</p>

<p><b>CAVEAT</b>: the input <i>$octets</i> might be modified in-place depending on what is set in CHECK. See <a href="Encode.html#LEAVE_SRC">&quot;LEAVE_SRC&quot;</a> if you want your inputs to be left unchanged.</p>

<h2 id="Listing-available-encodings"><a class="permalink" href="Encode.html#Listing-available-encodings">#</a><a id="Listing"></a>Listing available encodings</h2>

<pre><code>use Encode;
@list = Encode-&gt;encodings();</code></pre>

<p>Returns a list of canonical names of available encodings that have already been loaded. To get a list of all available encodings including those that have not yet been loaded, say:</p>

<pre><code>@all_encodings = Encode-&gt;encodings(&quot;:all&quot;);</code></pre>

<p>Or you can give the name of a specific module:</p>

<pre><code>@with_jp = Encode-&gt;encodings(&quot;Encode::JP&quot;);</code></pre>

<p>When &quot;<code>::</code>&quot; is not in the name, &quot;<code>Encode::</code>&quot; is assumed.</p>

<pre><code>@ebcdic = Encode-&gt;encodings(&quot;EBCDIC&quot;);</code></pre>

<p>To find out in detail which encodings are supported by this package, see <a href="./Encode::Supported.html">Encode::Supported</a>.</p>

<h2 id="Defining-Aliases"><a class="permalink" href="Encode.html#Defining-Aliases">#</a><a id="Defining"></a>Defining Aliases</h2>

<p>To add a new alias to a given encoding, use:</p>

<pre><code>use Encode;
use Encode::Alias;
define_alias(NEWNAME =&gt; ENCODING);</code></pre>

<p>After that, <i>NEWNAME</i> can be used as an alias for <i>ENCODING</i>. <i>ENCODING</i> may be either the name of an encoding or an <i>encoding object</i>.</p>

<p>Before you do that, first make sure the alias is nonexistent using <code>resolve_alias()</code>, which returns the canonical name thereof. For example:</p>

<pre><code>Encode::resolve_alias(&quot;latin1&quot;) eq &quot;iso-8859-1&quot; # true
Encode::resolve_alias(&quot;iso-8859-12&quot;)   # false; nonexistent
Encode::resolve_alias($name) eq $name  # true if $name is canonical</code></pre>

<p><code>resolve_alias()</code> does not need <code>use Encode::Alias</code>; it can be imported via <code>use Encode qw(resolve_alias)</code>.</p>

<p>See <a href="./Encode::Alias.html">Encode::Alias</a> for details.</p>

<h2 id="Finding-IANA-Character-Set-Registry-names"><a class="permalink" href="Encode.html#Finding-IANA-Character-Set-Registry-names">#</a><a id="Finding"></a>Finding IANA Character Set Registry names</h2>

<p>The canonical name of a given encoding does not necessarily agree with IANA Character Set Registry, commonly seen as <code>Content-Type: text/plain; charset=<i>WHATEVER</i></code>. For most cases, the canonical name works, but sometimes it does not, most notably with &quot;utf-8-strict&quot;.</p>

<p>As of <code>Encode</code> version 2.21, a new method <code>mime_name()</code> is therefore added.</p>

<pre><code>use Encode;
my $enc = find_encoding(&quot;UTF-8&quot;);
warn $enc-&gt;name;      # utf-8-strict
warn $enc-&gt;mime_name; # UTF-8</code></pre>

<p>See also: <a href="./Encode::Encoding.html">Encode::Encoding</a></p>

<h1 id="Encoding-via-PerlIO"><a class="permalink" href="Encode.html#Encoding-via-PerlIO">#</a><a id="Encoding"></a>Encoding via PerlIO</h1>

<p>If your perl supports <code>PerlIO</code> (which is the default), you can use a <code>PerlIO</code> layer to decode and encode directly via a filehandle. The following two examples are fully identical in functionality:</p>

<pre><code>### Version 1 via PerlIO
  open(INPUT,  &quot;&lt; :encoding(shiftjis)&quot;, $infile)
      || die &quot;Can&#39;t open &lt; $infile for reading: $!&quot;;
  open(OUTPUT, &quot;&gt; :encoding(euc-jp)&quot;,  $outfile)
      || die &quot;Can&#39;t open &gt; $output for writing: $!&quot;;
  while (&lt;INPUT&gt;) {   # auto decodes $_
      print OUTPUT;   # auto encodes $_
  }
  close(INPUT)   || die &quot;can&#39;t close $infile: $!&quot;;
  close(OUTPUT)  || die &quot;can&#39;t close $outfile: $!&quot;;

### Version 2 via from_to()
  open(INPUT,  &quot;&lt; :raw&quot;, $infile)
      || die &quot;Can&#39;t open &lt; $infile for reading: $!&quot;;
  open(OUTPUT, &quot;&gt; :raw&quot;,  $outfile)
      || die &quot;Can&#39;t open &gt; $output for writing: $!&quot;;

  while (&lt;INPUT&gt;) {
      from_to($_, &quot;shiftjis&quot;, &quot;euc-jp&quot;, 1);  # switch encoding
      print OUTPUT;   # emit raw (but properly encoded) data
  }
  close(INPUT)   || die &quot;can&#39;t close $infile: $!&quot;;
  close(OUTPUT)  || die &quot;can&#39;t close $outfile: $!&quot;;</code></pre>

<p>In the first version above, you let the appropriate encoding layer handle the conversion. In the second, you explicitly translate from one encoding to the other.</p>

<p>Unfortunately, it may be that encodings are not <code>PerlIO</code>-savvy. You can check to see whether your encoding is supported by <code>PerlIO</code> by invoking the <code>perlio_ok</code> method on it:</p>

<pre><code>Encode::perlio_ok(&quot;hz&quot;);             # false
find_encoding(&quot;euc-cn&quot;)-&gt;perlio_ok;  # true wherever PerlIO is available

use Encode qw(perlio_ok);            # imported upon request
perlio_ok(&quot;euc-jp&quot;)</code></pre>

<p>Fortunately, all encodings that come with <code>Encode</code> core are <code>PerlIO</code>-savvy except for <code>hz</code> and <code>ISO-2022-kr</code>. For the gory details, see <a href="./Encode::Encoding.html">Encode::Encoding</a> and <a href="./Encode::PerlIO.html">Encode::PerlIO</a>.</p>

<h1 id="Handling-Malformed-Data"><a class="permalink" href="Encode.html#Handling-Malformed-Data">#</a><a id="Handling"></a>Handling Malformed Data</h1>

<p>The optional <i>CHECK</i> argument tells <code>Encode</code> what to do when encountering malformed data. Without <i>CHECK</i>, <code>Encode::FB_DEFAULT</code> (== 0) is assumed.</p>

<p>As of version 2.12, <code>Encode</code> supports coderef values for <code>CHECK</code>; see below.</p>

<p><b>NOTE:</b> Not all encodings support this feature. Some encodings ignore the <i>CHECK</i> argument. For example, <a href="./Encode::Unicode.html">Encode::Unicode</a> ignores <i>CHECK</i> and it always croaks on error.</p>

<h2 id="List-of-CHECK-values"><a class="permalink" href="Encode.html#List-of-CHECK-values">#</a><a id="List"></a>List of <i>CHECK</i> values</h2>

<h3 id="FB_DEFAULT"><a class="permalink" href="Encode.html#FB_DEFAULT">#</a>FB_DEFAULT</h3>

<pre><code class="plaintext">I&lt;CHECK&gt; = Encode::FB_DEFAULT ( == 0)</code></pre>

<p>If <i>CHECK</i> is 0, encoding and decoding replace any malformed character with a <i>substitution character</i>. When you encode, <i>SUBCHAR</i> is used. When you decode, the Unicode REPLACEMENT CHARACTER, code point U+FFFD, is used. If the data is supposed to be UTF-8, an optional lexical warning of warning category <code>&quot;utf8&quot;</code> is given.</p>

<h3 id="FB_CROAK"><a class="permalink" href="Encode.html#FB_CROAK">#</a>FB_CROAK</h3>

<pre><code class="plaintext">I&lt;CHECK&gt; = Encode::FB_CROAK ( == 1)</code></pre>

<p>If <i>CHECK</i> is 1, methods immediately die with an error message. Therefore, when <i>CHECK</i> is 1, you should trap exceptions with <code>eval{}</code>, unless you really want to let it <code>die</code>.</p>

<h3 id="FB_QUIET"><a class="permalink" href="Encode.html#FB_QUIET">#</a>FB_QUIET</h3>

<pre><code class="plaintext">I&lt;CHECK&gt; = Encode::FB_QUIET</code></pre>

<p>If <i>CHECK</i> is set to <code>Encode::FB_QUIET</code>, encoding and decoding immediately return the portion of the data that has been processed so far when an error occurs. The data argument is overwritten with everything after that point; that is, the unprocessed portion of the data. This is handy when you have to call <code>decode</code> repeatedly in the case where your source data may contain partial multi-byte character sequences, (that is, you are reading with a fixed-width buffer). Here&#39;s some sample code to do exactly that:</p>

<pre><code>my($buffer, $string) = (&quot;&quot;, &quot;&quot;);
while (read($fh, $buffer, 256, length($buffer))) {
    $string .= decode($encoding, $buffer, Encode::FB_QUIET);
    # $buffer now contains the unprocessed partial character
}</code></pre>

<h3 id="FB_WARN"><a class="permalink" href="Encode.html#FB_WARN">#</a>FB_WARN</h3>

<pre><code class="plaintext">I&lt;CHECK&gt; = Encode::FB_WARN</code></pre>

<p>This is the same as <code>FB_QUIET</code> above, except that instead of being silent on errors, it issues a warning. This is handy for when you are debugging.</p>

<p><b>CAVEAT</b>: All warnings from Encode module are reported, independently of <a href="warnings.html">pragma warnings</a> settings. If you want to follow settings of lexical warnings configured by <a href="warnings.html">pragma warnings</a> then append also check value <code>ENCODE::ONLY_PRAGMA_WARNINGS</code>. This value is available since Encode version 2.99.</p>

<h3 id="FB_PERLQQ-FB_HTMLCREF-FB_XMLCREF"><a class="permalink" href="Encode.html#FB_PERLQQ-FB_HTMLCREF-FB_XMLCREF">#</a><a id="FB_PERLQQ"></a>FB_PERLQQ FB_HTMLCREF FB_XMLCREF</h3>

<dl>

<dt id="perlqq-mode-(CHECK-=-Encode::FB_PERLQQ)"><a class="permalink" href="Encode.html#perlqq-mode-(CHECK-=-Encode::FB_PERLQQ)">#</a><a id="perlqq"></a><a id="perlqq-mode-CHECK-Encode::FB_PERLQQ"></a>perlqq mode (<i>CHECK</i> = Encode::FB_PERLQQ)</dt>
<dd>

</dd>
<dt id="HTML-charref-mode-(CHECK-=-Encode::FB_HTMLCREF)"><a class="permalink" href="Encode.html#HTML-charref-mode-(CHECK-=-Encode::FB_HTMLCREF)">#</a><a id="HTML"></a><a id="HTML-charref-mode-CHECK-Encode::FB_HTMLCREF"></a>HTML charref mode (<i>CHECK</i> = Encode::FB_HTMLCREF)</dt>
<dd>

</dd>
<dt id="XML-charref-mode-(CHECK-=-Encode::FB_XMLCREF)"><a class="permalink" href="Encode.html#XML-charref-mode-(CHECK-=-Encode::FB_XMLCREF)">#</a><a id="XML"></a><a id="XML-charref-mode-CHECK-Encode::FB_XMLCREF"></a>XML charref mode (<i>CHECK</i> = Encode::FB_XMLCREF)</dt>
<dd>

</dd>
</dl>

<p>For encodings that are implemented by the <code>Encode::XS</code> module, <code>CHECK</code> <code>==</code> <code>Encode::FB_PERLQQ</code> puts <code>encode</code> and <code>decode</code> into <code>perlqq</code> fallback mode.</p>

<p>When you decode, <code>\x<i>HH</i></code> is inserted for a malformed character, where <i>HH</i> is the hex representation of the octet that could not be decoded to utf8. When you encode, <code>\x{<i>HHHH</i>}</code> will be inserted, where <i>HHHH</i> is the Unicode code point (in any number of hex digits) of the character that cannot be found in the character repertoire of the encoding.</p>

<p>The HTML/XML character reference modes are about the same. In place of <code>\x{<i>HHHH</i>}</code>, HTML uses <code>&amp;#<i>NNN</i>;</code> where <i>NNN</i> is a decimal number, and XML uses <code>&amp;#x<i>HHHH</i>;</code> where <i>HHHH</i> is the hexadecimal number.</p>

<p>In <code>Encode</code> 2.10 or later, <code>LEAVE_SRC</code> is also implied.</p>

<h3 id="The-bitmask"><a class="permalink" href="Encode.html#The-bitmask">#</a><a id="The"></a>The bitmask</h3>

<p>These modes are all actually set via a bitmask. Here is how the <code>FB_<i>XXX</i></code> constants are laid out. You can import the <code>FB_<i>XXX</i></code> constants via <code>use Encode qw(:fallbacks)</code>, and you can import the generic bitmask constants via <code>use Encode qw(:fallback_all)</code>.</p>

<pre><code class="plaintext">                    FB_DEFAULT FB_CROAK FB_QUIET FB_WARN  FB_PERLQQ
DIE_ON_ERR    0x0001             X
WARN_ON_ERR   0x0002                               X
RETURN_ON_ERR 0x0004                      X        X
LEAVE_SRC     0x0008                                        X
PERLQQ        0x0100                                        X
HTMLCREF      0x0200
XMLCREF       0x0400</code></pre>

<h3 id="LEAVE_SRC"><a class="permalink" href="Encode.html#LEAVE_SRC">#</a>LEAVE_SRC</h3>

<pre><code class="plaintext">Encode::LEAVE_SRC</code></pre>

<p>If the <code>Encode::LEAVE_SRC</code> bit is <i>not</i> set but <i>CHECK</i> is set, then the source string to encode() or decode() will be overwritten in place. If you&#39;re not interested in this, then bitwise-OR it with the bitmask.</p>

<h2 id="coderef-for-CHECK"><a class="permalink" href="Encode.html#coderef-for-CHECK">#</a><a id="coderef"></a>coderef for CHECK</h2>

<p>As of <code>Encode</code> 2.12, <code>CHECK</code> can also be a code reference which takes the ordinal value of the unmapped character as an argument and returns octets that represent the fallback character. For instance:</p>

<pre><code>$ascii = encode(&quot;ascii&quot;, $utf8, sub{ sprintf &quot;&lt;U+%04X&gt;&quot;, shift });</code></pre>

<p>Acts like <code>FB_PERLQQ</code> but U+<i>XXXX</i> is used instead of <code>\x{<i>XXXX</i>}</code>.</p>

<p>Fallback for <code>decode</code> must return decoded string (sequence of characters) and takes a list of ordinal values as its arguments. So for example if you wish to decode octets as UTF-8, and use ISO-8859-15 as a fallback for bytes that are not valid UTF-8, you could write</p>

<pre><code>$str = decode &#39;UTF-8&#39;, $octets, sub {
    my $tmp = join &#39;&#39;, map chr, @_;
    return decode &#39;ISO-8859-15&#39;, $tmp;
};</code></pre>

<h1 id="Defining-Encodings"><a class="permalink" href="Encode.html#Defining-Encodings">#</a><a id="Defining1"></a>Defining Encodings</h1>

<p>To define a new encoding, use:</p>

<pre><code>use Encode qw(define_encoding);
define_encoding($object, CANONICAL_NAME [, alias...]);</code></pre>

<p><i>CANONICAL_NAME</i> will be associated with <i>$object</i>. The object should provide the interface described in <a href="./Encode::Encoding.html">Encode::Encoding</a>. If more than two arguments are provided, additional arguments are considered aliases for <i>$object</i>.</p>

<p>See <a href="./Encode::Encoding.html">Encode::Encoding</a> for details.</p>

<h1 id="The-UTF8-flag"><a class="permalink" href="Encode.html#The-UTF8-flag">#</a><a id="The1"></a>The UTF8 flag</h1>

<p>Before the introduction of Unicode support in Perl, The <code>eq</code> operator just compared the strings represented by two scalars. Beginning with Perl 5.8, <code>eq</code> compares two strings with simultaneous consideration of <i>the UTF8 flag</i>. To explain why we made it so, I quote from page 402 of <i>Programming Perl, 3rd ed.</i></p>

<dl>

<dt id="Goal-#1:"><a class="permalink" href="Encode.html#Goal-%231:">#</a><a id="Goal"></a><a id="Goal-1"></a>Goal #1:</dt>
<dd>

<p>Old byte-oriented programs should not spontaneously break on the old byte-oriented data they used to work on.</p>

</dd>
<dt id="Goal-#2:"><a class="permalink" href="Encode.html#Goal-%232:">#</a><a id="Goal1"></a><a id="Goal-2"></a>Goal #2:</dt>
<dd>

<p>Old byte-oriented programs should magically start working on the new character-oriented data when appropriate.</p>

</dd>
<dt id="Goal-#3:"><a class="permalink" href="Encode.html#Goal-%233:">#</a><a id="Goal2"></a><a id="Goal-3"></a>Goal #3:</dt>
<dd>

<p>Programs should run just as fast in the new character-oriented mode as in the old byte-oriented mode.</p>

</dd>
<dt id="Goal-#4:"><a class="permalink" href="Encode.html#Goal-%234:">#</a><a id="Goal3"></a><a id="Goal-4"></a>Goal #4:</dt>
<dd>

<p>Perl should remain one language, rather than forking into a byte-oriented Perl and a character-oriented Perl.</p>

</dd>
</dl>

<p>When <i>Programming Perl, 3rd ed.</i> was written, not even Perl 5.6.0 had been born yet, many features documented in the book remained unimplemented for a long time. Perl 5.8 corrected much of this, and the introduction of the UTF8 flag is one of them. You can think of there being two fundamentally different kinds of strings and string-operations in Perl: one a byte-oriented mode for when the internal UTF8 flag is off, and the other a character-oriented mode for when the internal UTF8 flag is on.</p>

<p>This UTF8 flag is not visible in Perl scripts, exactly for the same reason you cannot (or rather, you <i>don&#39;t have to</i>) see whether a scalar contains a string, an integer, or a floating-point number. But you can still peek and poke these if you will. See the next section.</p>

<h2 id="Messing-with-Perl&#39;s-Internals"><a class="permalink" href="Encode.html#Messing-with-Perl&#39;s-Internals">#</a><a id="Messing"></a><a id="Messing-with-Perls-Internals"></a>Messing with Perl&#39;s Internals</h2>

<p>The following API uses parts of Perl&#39;s internals in the current implementation. As such, they are efficient but may change in a future release.</p>

<h3 id="is_utf8"><a class="permalink" href="Encode.html#is_utf8">#</a>is_utf8</h3>

<pre><code class="plaintext">is_utf8(STRING [, CHECK])</code></pre>

<p>[INTERNAL] Tests whether the UTF8 flag is turned on in the <i>STRING</i>. If <i>CHECK</i> is true, also checks whether <i>STRING</i> contains well-formed UTF-8. Returns true if successful, false otherwise.</p>

<p>Typically only necessary for debugging and testing. Don&#39;t use this flag as a marker to distinguish character and binary data, that should be decided for each variable when you write your code.</p>

<p><b>CAVEAT</b>: If <i>STRING</i> has UTF8 flag set, it does <b>NOT</b> mean that <i>STRING</i> is UTF-8 encoded and vice-versa.</p>

<p>As of Perl 5.8.1, <a href="utf8.html">utf8</a> also has the <code>utf8::is_utf8</code> function.</p>

<h3 id="_utf8_on"><a class="permalink" href="Encode.html#_utf8_on">#</a><a id="utf8_on"></a>_utf8_on</h3>

<pre><code class="plaintext">_utf8_on(STRING)</code></pre>

<p>[INTERNAL] Turns the <i>STRING</i>&#39;s internal UTF8 flag <b>on</b>. The <i>STRING</i> is <i>not</i> checked for containing only well-formed UTF-8. Do not use this unless you <i>know with absolute certainty</i> that the STRING holds only well-formed UTF-8. Returns the previous state of the UTF8 flag (so please don&#39;t treat the return value as indicating success or failure), or <code>undef</code> if <i>STRING</i> is not a string.</p>

<p><b>NOTE</b>: For security reasons, this function does not work on tainted values.</p>

<h3 id="_utf8_off"><a class="permalink" href="Encode.html#_utf8_off">#</a><a id="utf8_off"></a>_utf8_off</h3>

<pre><code class="plaintext">_utf8_off(STRING)</code></pre>

<p>[INTERNAL] Turns the <i>STRING</i>&#39;s internal UTF8 flag <b>off</b>. Do not use frivolously. Returns the previous state of the UTF8 flag, or <code>undef</code> if <i>STRING</i> is not a string. Do not treat the return value as indicative of success or failure, because that isn&#39;t what it means: it is only the previous setting.</p>

<p><b>NOTE</b>: For security reasons, this function does not work on tainted values.</p>

<h1 id="UTF-8-vs.-utf8-vs.-UTF8"><a class="permalink" href="Encode.html#UTF-8-vs.-utf8-vs.-UTF8">#</a><a id="UTF"></a>UTF-8 vs. utf8 vs. UTF8</h1>

<pre><code class="plaintext">....We now view strings not as sequences of bytes, but as sequences
of numbers in the range 0 .. 2**32-1 (or in the case of 64-bit
computers, 0 .. 2**64-1) -- Programming Perl, 3rd ed.</code></pre>

<p>That has historically been Perl&#39;s notion of UTF-8, as that is how UTF-8 was first conceived by Ken Thompson when he invented it. However, thanks to later revisions to the applicable standards, official UTF-8 is now rather stricter than that. For example, its range is much narrower (0 .. 0x10_FFFF to cover only 21 bits instead of 32 or 64 bits) and some sequences are not allowed, like those used in surrogate pairs, the 31 non-character code points 0xFDD0 .. 0xFDEF, the last two code points in <i>any</i> plane (0x<i>XX</i>_FFFE and 0x<i>XX</i>_FFFF), all non-shortest encodings, etc.</p>

<p>The former default in which Perl would always use a loose interpretation of UTF-8 has now been overruled:</p>

<pre><code>From: Larry Wall &lt;larry@wall.org&gt;
Date: December 04, 2004 11:51:58 JST
To: perl-unicode@perl.org
Subject: Re: Make Encode.pm support the real UTF-8
Message-Id: &lt;20041204025158.GA28754@wall.org&gt;

On Fri, Dec 03, 2004 at 10:12:12PM +0000, Tim Bunce wrote:
: I&#39;ve no problem with &#39;utf8&#39; being perl&#39;s unrestricted uft8 encoding,
: but &quot;UTF-8&quot; is the name of the standard and should give the
: corresponding behaviour.

For what it&#39;s worth, that&#39;s how I&#39;ve always kept them straight in my
head.

Also for what it&#39;s worth, Perl 6 will mostly default to strict but
make it easy to switch back to lax.

Larry</code></pre>

<p>Got that? As of Perl 5.8.7, <b>&quot;UTF-8&quot;</b> means UTF-8 in its current sense, which is conservative and strict and security-conscious, whereas <b>&quot;utf8&quot;</b> means UTF-8 in its former sense, which was liberal and loose and lax. <code>Encode</code> version 2.10 or later thus groks this subtle but critically important distinction between <code>&quot;UTF-8&quot;</code> and <code>&quot;utf8&quot;</code>.</p>

<pre><code>encode(&quot;utf8&quot;,  &quot;\x{FFFF_FFFF}&quot;, 1); # okay
encode(&quot;UTF-8&quot;, &quot;\x{FFFF_FFFF}&quot;, 1); # croaks</code></pre>

<p>In the <code>Encode</code> module, <code>&quot;UTF-8&quot;</code> is actually a canonical name for <code>&quot;utf-8-strict&quot;</code>. That hyphen between the <code>&quot;UTF&quot;</code> and the <code>&quot;8&quot;</code> is critical; without it, <code>Encode</code> goes &quot;liberal&quot; and (perhaps overly-)permissive:</p>

<pre><code>find_encoding(&quot;UTF-8&quot;)-&gt;name # is &#39;utf-8-strict&#39;
find_encoding(&quot;utf-8&quot;)-&gt;name # ditto. names are case insensitive
find_encoding(&quot;utf_8&quot;)-&gt;name # ditto. &quot;_&quot; are treated as &quot;-&quot;
find_encoding(&quot;UTF8&quot;)-&gt;name  # is &#39;utf8&#39;.</code></pre>

<p>Perl&#39;s internal UTF8 flag is called &quot;UTF8&quot;, without a hyphen. It indicates whether a string is internally encoded as &quot;utf8&quot;, also without a hyphen.</p>

<h1 id="SEE-ALSO"><a class="permalink" href="Encode.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p><a href="./Encode::Encoding.html">Encode::Encoding</a>, <a href="./Encode::Supported.html">Encode::Supported</a>, <a href="./Encode::PerlIO.html">Encode::PerlIO</a>, <a href="encoding.html">encoding</a>, <a href="perlebcdic.html">perlebcdic</a>, <a href="perlfunc.html#open">&quot;open&quot; in perlfunc</a>, <a href="perlunicode.html">perlunicode</a>, <a href="perluniintro.html">perluniintro</a>, <a href="perlunifaq.html">perlunifaq</a>, <a href="perlunitut.html">perlunitut</a> <a href="utf8.html">utf8</a>, the Perl Unicode Mailing List <a href="http://lists.perl.org/list/perl-unicode.html">http://lists.perl.org/list/perl-unicode.html</a></p>

<h1 id="MAINTAINER"><a class="permalink" href="Encode.html#MAINTAINER">#</a>MAINTAINER</h1>

<p>This project was originated by the late Nick Ing-Simmons and later maintained by Dan Kogai <i>&lt;dankogai@cpan.org&gt;</i>. See AUTHORS for a full list of people involved. For any questions, send mail to <i>&lt;perl-unicode@perl.org&gt;</i> so that we can all share.</p>

<p>While Dan Kogai retains the copyright as a maintainer, credit should go to all those involved. See AUTHORS for a list of those who submitted code to the project.</p>

<h1 id="COPYRIGHT"><a class="permalink" href="Encode.html#COPYRIGHT">#</a>COPYRIGHT</h1>

<p>Copyright 2002-2014 Dan Kogai <i>&lt;dankogai@cpan.org&gt;</i>.</p>

<p>This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
